package stz.flash;

import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.FontManager;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import stz.flash.main.BacklightOnOffListener;
import stz.flash.main.FlashScreen;
import stz.flash.main.ModeChangeHandler;
import stz.flash.main.ScreenHandle;
import stz.flash.main.strobe.model.GlobalKeyListenerOfMainApp;
import stz.flash.option.PersistentData;


public class MainApp extends UiApplication {
	
	public static final int APP_FONT_HEIGHT = Ui.convertSize(8, Ui.UNITS_pt, Ui.UNITS_px);
	private static final String APP_FONT_NAME = "Arial";
	
	public MainApp() {
		
		addSystemListener(new BacklightOnOffListener());
		addKeyListener(new GlobalKeyListenerOfMainApp(), false);//true이면 글로벌키가 작동안한다
		
		ScreenHandle.getIns().setScreen(new FlashScreen());
		
		if (ApplicationManager.getApplicationManager().inStartup() == false)
			loadStartupMode();
		
		try {
			FontManager.getInstance().setApplicationFont(
					FontFamily.forName(APP_FONT_NAME).getFont(Font.PLAIN, APP_FONT_HEIGHT));
		} catch (ClassNotFoundException e) {
		}
		
		ModeChangeHandler.initMode();
	}
	
	protected boolean acceptsForeground() {
		if (isEventThread())
			loadStartupMode();
		return false;
	}

	/**
	 * 메뉴를 누르면 acceptsForeground()를 다량으로 호출하는
	 * 버그 앱 덕뿐에 if (screenhandle.isDisplayed())를 추가함
	 */
	private void loadStartupMode() {
		
		PersistentData.getPersistentDatabase().setTestActivity(false);
		
		if (ScreenHandle.getIns().isDisplayed() == false) {
			ModeChangeHandler.loadStartupMode();
		}
	}
}
